﻿using System.Linq;
using System.Windows;
using System.Windows.Data;
using Representation=Asistente.Model.Representation;

namespace Asistente
{
    /// <summary>
    /// Interaction logic for SingleEntitySelection.xaml
    /// </summary>
    public partial class SingleEntitySelection
    {
        private readonly SingleEntitySelectionViewModel viewModel;

        public SingleEntitySelection()
        {
            InitializeComponent();
            ObjectDataProvider provider = Resources["viewModel"] as ObjectDataProvider;
            this.viewModel = provider != null ? provider.ObjectInstance as SingleEntitySelectionViewModel : null;
        }

        void PopulateEntities(Representation.Application app)
        {
            if (viewModel != null)
            {
                viewModel.PopulateEntities(app);
            }
        }

        Representation.Entity GetSelectedEntity()
        {
            return viewModel.Entities.FirstOrDefault(entity => entity.IsChecked);
        }

        public static Representation.Entity ShowModal(Representation.Application app)
        {
            SingleEntitySelection dialog = new SingleEntitySelection();
            dialog.PopulateEntities(app);
            if (dialog.ShowDialog() ?? true)
            {
                return dialog.GetSelectedEntity();    
            }
            return null;
        }

        private void ButtonOkClick(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }

        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
